package com.itheima.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.POJO.CheckItem;
import com.itheima.POJO.HealthScheme;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.HealthSchemeMapper;
import com.itheima.dao.checkItemMapper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.HealthSchemeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service(interfaceClass = HealthSchemeService.class)
@Transactional
public class HealthSchemeServiceImpl implements HealthSchemeService {

    @Autowired
    private HealthSchemeMapper healthSchemeMapper;

    /**
     * 添加健康方案的服务提供者
     * @param healthScheme
     * @return
     */
    @Override
    public Result addHealthScheme(HealthScheme healthScheme) {
        try {
            healthSchemeMapper.addHealthScheme(healthScheme);
        } catch (Exception e) {
            e.printStackTrace();
            Result result = new Result(false,"添加失败请重试!");
            return  result;
        }
        return new Result(true,"添加成功!");
    }

    /**
     * 根据前端传过来的 当前页 每页显示的条数 和 查询的条件
     *
     * 去获取检查项 信息
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //下面这步其实多此一举 因为前端定义当前页默认为1 了!
        if (currentPage==null||currentPage.equals("")){
            currentPage=1;
        }
        PageHelper.startPage(currentPage,pageSize);

        //增加queryString2后再删除,queryString2的值消失
        //分割queryString 默认 "null-null" 或 "-null" 或 "null-"  或 "-"
        //分割后strings 长度为2 或2 或1 或0
        String[] strings = queryString.split("-");

        String queryString2 = null;
        String queryScheme = null;

        //queryString 为 "null-null" 或 "-null"
        if(strings.length == 2){
             queryString2 = strings[0];
             queryScheme = strings[1];
           //  queryString 为  "-null"
             if (queryString2.equals("")){
                 queryString2 = null;
             }
        }

        //queryString 为 "null-"
        if(strings.length == 1){
            queryString2 = strings[0];
            queryScheme =  null;
        }
        //queryString 为 "-",最后一种不用判断了.都为null


        Page<HealthScheme> page= healthSchemeMapper.findPage1(queryString2,queryScheme);

        //前台那边有当前页,每页显示的条数,我需要再给前台总记录数,和本页要展示的数据
        /*
         private Long total;//总记录数
         private List rows;//当前页结果
        * */
       PageResult pageResult= new PageResult(page.getTotal(),page.getResult());
       return pageResult;
    }

    /**
     * 根据id去删除检查项
     * @param id
     */
    @Override
    public void deleteHealthScheme(Integer id) {
        System.out.println("Service::" + id);
        healthSchemeMapper.delectHealthSchemeById(id);
    }


    /**
     * 根据id找到scheme
     * @param id
     * @return
     */
    @Override
    public Result findHealthSchemeById(Integer id) {
        try {
            HealthScheme healthScheme = healthSchemeMapper.findHealthSchemeById(id);
            return new Result(true, MessageConstant.QUERY_SCHEME_SUCCESS, healthScheme);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,"获取数据失败,请刷新当前页面");
        }

    }

    /**
     * 修改检查项
     * @param healthScheme
     */
    @Override
    public void updateHealthScheme(HealthScheme healthScheme) {

        Long aLong = healthSchemeMapper.updateHealthScheme (healthScheme);
        System.out.println("along==="+aLong);
        // aLong=0L;
        if (aLong==0){
            throw new RuntimeException(MessageConstant.EDIT_SCHEME_FAIL);
        }
    }

    public void forbiddenById(Integer id, String state) {
        //判断前台传来的status  如果是 0 就转换为1 进行更新数据
        if ("1".equals(state)) {
            state = "0";
        } else {
            state = "1";
        }
        healthSchemeMapper.updateHealthSchemeByStatus(id, state);
    }
}
